<template>
  <!--回款报表-->
  <section class="page payment-return-rate-report">
    <data-form :submitLoading="submitLoading" :model="searchModel" @onSearch="refreshData" @handleResetForm="handleResetForm">
      <template slot="inputs">
        <el-form-item label="逾期时间" prop="queryDate">
          <el-date-picker type="daterange" :editable="false" v-model="searchModel.queryDate" placeholder="选择日期时间" clearable :value-format="'yyyy-MM-dd'"></el-date-picker>
        </el-form-item>
        <el-form-item label="逾期天数" prop="overdueDays">
          <number-range v-model="searchModel.overdueDays" :isInteger="true"></number-range>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="dataSet" rowSpanKey="deptId" :rowClassName="rowClassName">
      <template slot="buttons">
        <el-button @click="exportCaseInfo" v-auth="2009">导出</el-button>
      </template>
      <template slot="columns">
          <el-table-column label="逾期时间" prop="overdueDate" show-overflow-tooltip></el-table-column>
          <el-table-column label="逾期阶段" prop="overduePeriod"></el-table-column>
          <el-table-column label="逾期金额" prop="overdueAmount" :formatter="row => $filter.toThousands(row.overdueAmount)"></el-table-column>
          <el-table-column label="逾期笔数" prop="overdueNum"></el-table-column>
          <el-table-column label="逾期人数" prop="overduePersonal"></el-table-column>
          <el-table-column label="清收金额" prop="repayAmount"></el-table-column>
          <el-table-column label="回款率" prop="returnRate">
            <template slot-scope="scope">
              <span>{{scope.row.returnRate?$filter.toPercent(scope.row.returnRate):"0.00%"}}</span>
            </template>
          </el-table-column>
      </template>
    </data-box>

  </section>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Auth, Layout, Dependencies } from '~/core/decorator';
import DataBox from '~/components/common/data-box.vue';
import DataForm from "~/components/common/data-form.vue"
import { ReportManageService } from "~/services/domain-service/report-manage.service";
import { UserService } from "~/services/management-service/user.service";
import SelectDepartment from "~/components/business-component/select-department.vue";
import { switchMap } from 'rxjs/operator/switchMap';
import NumberRange from "~/components/common/number-range.vue";
// @Auth(1004)
@Layout('workspace')
@Component({
  components: {
    DataBox,
    DataForm,
    SelectDepartment,
    NumberRange
  }
})
export default class PaymentReturnRateReport extends Vue {
  @Dependencies(ReportManageService) private reportManageService: ReportManageService;
  @Dependencies(UserService) private userService: UserService;
  private groupKey = "deptId"
  private searchModel: any = {
    queryDate:"",
    overdueDays: {
      max: "",
      min: ""
    },
  }
  private dataSet: any = []
  private userSet: any[] = []
  private submitLoading: any = true

  private refreshData() {
    let temp =''
    const temps:any = []
    //当点击逾期时间查询后，在表格中显示所选逾期时间
    if(this.searchModel.queryDate){
      this.searchModel.queryDate.map(x=>{
        temps.push(x)
      })
      temp = temps.join("~")
    }
    this.reportManageService.queryMoneyBackRate(this.searchModel)
      .subscribe(data => {
        console.log(data,"data")
        // if(data.length!==0){
        //   data.map(x=>{
        //     x.overdueDate = temp
        //     switch(x.overduePeriod) {
        //       case 1:
        //         x.overduePeriod = "M1"
        //         break
        //       case 2:
        //         x.overduePeriod = "M2"
        //         break
        //       case 3:
        //         x.overduePeriod = "M3"
        //         break
        //       case 4:
        //         x.overduePeriod = "M4-M12"
        //         break
        //       case 12:
        //         x.overduePeriod = "M12+"
        //         break
        //       default:
        //         break
        //     }
        //   })
           this.dataSet = data
           if(this.dataSet){
            this.dataSet.forEach(x=>{
              x.overdueDate = temp
              x.overduePeriod>12?x.overduePeriod = 'M12+':x.overduePeriod = 'M'+x.overduePeriod
            })
          }
           this.submitLoading = false
           return
        
      })
  }

  /**
   * 统计数据
   */
//   private groupData() {
//     // 1.统计关联键的值
//     const values = this.dataSet.map(v => v[this.groupKey])

//     const set = new Set<string>(values)
//     set.forEach(id => {
//       const filterRows = this.dataSet.filter(x => x[this.groupKey] === id)
//       const repayAmount = filterRows.reduce((s, c) => s + c.repayAmount, 0)
//       const totalValue = {
//         deptId: id,
//         collectorRealName: `合计：${filterRows.length}`,
//         repayAmount,
//         totalFlag: true
//       }
//       this.dataSet.push(totalValue)
//     })
//   }

  private rowClassName({ row }) {
    if (row.totalFlag) return 'total'
  }

  private departemntChange(id,code) {
    this.searchModel.deptCode = code
    this.searchModel.userId = ''
    if (!code) {
      this.userSet = []
    } else {
      this.getAllUserByDeptCode(code)
    }
  }
  //案件导出
  private exportCaseInfo() {
    if(this.dataSet.length === 0){
      this.$message("暂无数据可以导出")
      return
    }
    this.reportManageService
      .exportMoneyBackRate(this.searchModel)
      .subscribe(data => this.$common.downloadFile(data.url));
  }

  private getAllUserByDeptCode(deptCode: string) {
    this.userService.getAllUsersList({deptCode}).subscribe(
      data => this.userSet = data
    )
  }

  private handleResetForm() {
    this.searchModel.deptCode = ''
  }
  private mounted() {
    this.refreshData()
  }

}
</script>

<style lang="less">
.page.repayment-report {
  .data-box {
    .el-table__row {
      &.total {
        font-weight: bold;
        font-size: 16px;
      }
    }
  }
}
</style>
